#![allow(unused)]
use axum::http::StatusCode;
use serde_json::json;

pub type R<T> = Result<axum::Json<T>, HttpError>;

#[derive(Debug, Clone)]
pub struct HttpError {
    status_code: StatusCode,
    message: String,
}

impl HttpError {
    pub fn new(status_code: StatusCode, message: impl Into<String>) -> HttpError {
        HttpError {
            status_code,
            message: message.into(),
        }
    }

    pub fn bad_request(message: impl Into<String>) -> HttpError {
        HttpError {
            status_code: StatusCode::BAD_REQUEST,
            message: message.into(),
        }
    }

    pub fn server_error(message: impl Into<String>) -> HttpError {
        HttpError {
            status_code: StatusCode::INTERNAL_SERVER_ERROR,
            message: message.into(),
        }
    }
}

impl axum::response::IntoResponse for HttpError {
    fn into_response(self) -> axum::response::Response {
        let json_message = json!({
            "message": self.message,
        });

        (self.status_code, axum::Json(json_message)).into_response()
    }
}
